//
//  ListMesViewController.m
//  xmppones
//
//  Created by 李立达 on 2017/2/21.
//  Copyright © 2017年 李立达. All rights reserved.
//

#import "ListMesViewController.h"
#import "xmppmannger.h"
#import "listTableViewCell.h"
#import "ChatViewController.h"
@interface ListMesViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableview;
@property (nonatomic,strong)NSMutableArray     *listcontact;
@property (nonatomic,assign)BOOL isfirstLoad;
@end
#define listcellheight 60
@implementation ListMesViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if(_isfirstLoad)
    {
        [self reloadData:nil];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isfirstLoad =YES;
    self.listcontact=[NSMutableArray array];
    [self creatUI];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadData:) name:REFRESHFRIEND object:nil];
}

-(void)creatUI
{
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, NavgionbarHeight, KDeviceWith, KDeviceHeight-49-NavgionbarHeight) style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.rowHeight = listcellheight;
    self.tableview.backgroundColor = [UIColor whiteColor];
    [self.tableview registerClass:[listTableViewCell class] forCellReuseIdentifier:@"listcell"];
    [self.view addSubview:self.tableview];
}

-(void)reloadData:(NSNotification *)noti
{
    self.listcontact =[NSMutableArray arrayWithArray:[xmppmannger shareInitOnes].xmpprostermemorystorage.unsortedUsers ];
    [self.tableview reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listcontact.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    listTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listcell"];
    XMPPUserMemoryStorageObject *user = self.listcontact[indexPath.row];

    cell.photo.image = user.photo;
    cell.Nametitle.text = user.jid.user;
    [cell.Nametitle sizeToFit];
    cell.Nametitle.center = CGPointMake(cell.photo.right + 20 + cell.Nametitle.width/2, listcellheight/2);
    if([user isOnline])
    {
        cell.status.text = @"在线";
    }else
    {
        cell.status.text = @"离线";
    }
    [cell.status sizeToFit];
    cell.status.center = CGPointMake(KDeviceWith-20-cell.status.width/2, listcellheight/2);
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ChatViewController *chat = [[ChatViewController alloc]init];
    [self.navigationController pushViewController:chat animated:YES];
}

-(UIButton *)returnLeftButton
{
    return nil;
}

@end
